package com.shrct.businessprocess.purchase.impl;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.RCBudgetStatus;
import com.shrct.businessprocess.budget.RCBudgetTaskStatus;
import com.shrct.businessprocess.budget.RCCost4ACTItem;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.budget.RCHasCostBudgetCostRequest;
import com.shrct.businessprocess.budget.RCProductDirectCostRequest;
import com.shrct.businessprocess.budget.RCProductDirectCostRequestType;
import com.shrct.businessprocess.budget.impl.RCProductDirectCostRequestImpl;
import com.shrct.businessprocess.diagnose.DiagnoseItem;
import com.shrct.businessprocess.diagnose.IDiagnoseAcceptor;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.product.RCProductEntry;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.purchase.IHasAcceptedProducts;
import com.shrct.businessprocess.purchase.RCBaseProductPurchaseBudget;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrder;
import com.shrct.businessprocess.purchase.RCBasePurchasePayRequest;
import com.shrct.businessprocess.purchase.RCBasePurchaseRequest;
import com.shrct.businessprocess.purchase.RCBudgetContract;
import com.shrct.businessprocess.purchase.RCBudgetContractItem;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCOrderCommissionPayRequest;
import com.shrct.businessprocess.purchase.RCPay;
import com.shrct.businessprocess.purchase.RCPayRequest;
import com.shrct.businessprocess.purchase.RCProductDeliver;
import com.shrct.businessprocess.purchase.RCProductDeliverItem;
import com.shrct.businessprocess.purchase.RCProductInStockEvent;
import com.shrct.businessprocess.purchase.RCProductPurchaseItem;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequest;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequestItem;
import com.shrct.businessprocess.purchase.RCProductPurchaseRequestStatus;
import com.shrct.businessprocess.purchase.RCPurchaseOrder;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import com.shrct.businessprocess.purchase.RCServiceProductPurchaseRequest;
import com.shrct.businessprocess.purchase.RCServicePurchaseOrder;
import com.shrct.businessprocess.purchase.RCTransforCostPayRequest;
import com.shrct.businessprocess.purchase.impl.RCHasPurchaseBudgetImpl;
import com.shrct.businessprocess.purchase.impl.RCProductPurchaseItemImpl;
import com.shrct.businessprocess.purchase.impl.RCProductPurchaseRequestImpl;
import com.shrct.businessprocess.purchase.impl.RCTransforCostPayRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCBaseProductPurchaseBudgetProxy;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCBASEPRODUCTPURCHASEBUDGET_TYPE")
public abstract class RCBaseProductPurchaseBudgetImpl extends RCHasPurchaseBudgetImpl implements RCBaseProductPurchaseBudget {
  public RCBaseProductPurchaseBudgetImpl() {
    super();
  }
  
  public RCBaseProductPurchaseBudgetImpl(final RCCostBreakDown cbd) {
    super( cbd);
    
  }
  
  @OneToMany(targetEntity = RCTransforCostPayRequestImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "budget")
  private Collection<RCTransforCostPayRequest> transforCostPayRequest = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCTransforCostPayRequest>();;
  
  public Collection<RCTransforCostPayRequest> getTransforCostPayRequest() {
    return this.transforCostPayRequest;
  }
  
  public RCTransforCostPayRequest createAndAddtoTransforCostPayRequest() {
    getTransforCostPayRequest().size();
    com.shrct.businessprocess.purchase.RCTransforCostPayRequest rCTransforCostPayRequest = new com.shrct.businessprocess.purchase.impl.RCTransforCostPayRequestImpl(this);
    getObjectFactory().create(rCTransforCostPayRequest);
    getTransforCostPayRequest().add(rCTransforCostPayRequest);
    return rCTransforCostPayRequest;
    
  }
  
  public RCTransforCostPayRequest createAndAddtoTransforCostPayRequest(final Procedure1<RCTransforCostPayRequest> updater) {
    getTransforCostPayRequest().size();
    com.shrct.businessprocess.purchase.RCTransforCostPayRequest rCTransforCostPayRequest = new com.shrct.businessprocess.purchase.impl.RCTransforCostPayRequestImpl(this);
    getObjectFactory().create(rCTransforCostPayRequest);
    getTransforCostPayRequest().add(rCTransforCostPayRequest);
    return rCTransforCostPayRequest;
    
  }
  
  public RCBaseProductPurchaseBudget removeAllTransforCostPayRequest() {
    for(RCTransforCostPayRequest obj : this.transforCostPayRequest){
    	getObjectFactory().delete(obj);
    }
    this.transforCostPayRequest.clear();
    return this;	
    
  }
  
  public RCBaseProductPurchaseBudget removeFromTransforCostPayRequest(final RCTransforCostPayRequest rCTransforCostPayRequest) {
    this.transforCostPayRequest.remove(rCTransforCostPayRequest);
    getObjectFactory().delete(rCTransforCostPayRequest);
    return this;
    
  }
  
  public Collection<? extends RCPayRequest> getAllPayRequests() {
    Collection<? extends RCPayRequest> _allPayRequests = super.getAllPayRequests();
    Iterable<RCPayRequest> _plus = Iterables.<RCPayRequest>concat(this.transforCostPayRequest, _allPayRequests);
    List<RCPayRequest> _list = IterableExtensions.<RCPayRequest>toList(_plus);
    return _list;
  }
  
  public RCProductPurchaseRequest createProductPurchaseRequest() {RCProductPurchaseRequest rCProductPurchaseRequest=RCProductPurchaseRequestImpl.create(this,getObjectFactory());
    final Procedure1<RCProductPurchaseRequest> _function = new Procedure1<RCProductPurchaseRequest>() {
        public void apply(final RCProductPurchaseRequest it) {
          Date _day = DateUtil.today();
          it.setDate(_day);
        }
      };
    RCProductPurchaseRequest _doubleArrow = ObjectExtensions.<RCProductPurchaseRequest>operator_doubleArrow(rCProductPurchaseRequest, _function);
    return _doubleArrow;
  }
  
  /**
   * 获取产品清单
   */
  public Collection<RCProductPurchaseItem> getItems() {
    Collection<RCProductSpecification> _allProductSpecifications = this.getAllProductSpecifications();
    final Function1<RCProductSpecification,RCProductPurchaseItem> _function = new Function1<RCProductSpecification,RCProductPurchaseItem>() {
        public RCProductPurchaseItem apply(final RCProductSpecification productSpec) {
          RCProductPurchaseItemImpl _rCProductPurchaseItemImpl = new RCProductPurchaseItemImpl();
          final Procedure1<RCProductPurchaseItem> _function = new Procedure1<RCProductPurchaseItem>() {
              public void apply(final RCProductPurchaseItem it) {
                it.setProductSpec(productSpec);
                Integer _totalRequiredQuantity = RCBaseProductPurchaseBudgetImpl.this.getTotalRequiredQuantity(productSpec);
                it.setQuantity(_totalRequiredQuantity);
                RCProjectMoney _totalRequiredAmount = RCBaseProductPurchaseBudgetImpl.this.getTotalRequiredAmount(productSpec);
                it.setAmount(_totalRequiredAmount);
                Integer _totalRequestedQuantity = RCBaseProductPurchaseBudgetImpl.this.getTotalRequestedQuantity(productSpec);
                it.setRequestQuantity(_totalRequestedQuantity);
                Integer _totalOrderedQuantity = RCBaseProductPurchaseBudgetImpl.this.getTotalOrderedQuantity(productSpec);
                it.setOrderQuantity(_totalOrderedQuantity);
                Integer _totalAcceptedQuantity = RCBaseProductPurchaseBudgetImpl.this.getTotalAcceptedQuantity(productSpec);
                it.setAcceptQuantity(_totalAcceptedQuantity);
                Integer _totalDeliveredQuantity = RCBaseProductPurchaseBudgetImpl.this.getTotalDeliveredQuantity(productSpec);
                it.setDeliveredQuantity(_totalDeliveredQuantity);
              }
            };
          RCProductPurchaseItem _doubleArrow = ObjectExtensions.<RCProductPurchaseItem>operator_doubleArrow(((RCProductPurchaseItem) _rCProductPurchaseItemImpl), _function);
          return _doubleArrow;
        }
      };
    Iterable<RCProductPurchaseItem> _map = IterableExtensions.<RCProductSpecification, RCProductPurchaseItem>map(_allProductSpecifications, _function);
    List<RCProductPurchaseItem> _list = IterableExtensions.<RCProductPurchaseItem>toList(_map);
    return _list;
  }
  
  public Boolean seemsFinished() {
    boolean _xifexpression = false;
    RCBudgetStatus _budgetStatus = this.getBudgetStatus();
    boolean _equals = Objects.equal(_budgetStatus, RCBudgetStatus.unset);
    if (_equals) {
      boolean _xblockexpression = false;
      {
        final Collection<RCProductPurchaseItem> items = this.getItems();
        boolean _xifexpression_1 = false;
        boolean _and = false;
        int _size = items.size();
        boolean _equals_1 = (_size == 0);
        if (!_equals_1) {
          _and = false;
        } else {
          boolean _or = false;
          RCProjectMoney _contractPrice = this.getContractPrice();
          Double _amount = _contractPrice.getAmount();
          boolean _notEquals = ((_amount).doubleValue() != 0d);
          if (_notEquals) {
            _or = true;
          } else {
            RCProjectMoney _netPrice = this.getNetPrice();
            Double _amount_1 = _netPrice.getAmount();
            boolean _notEquals_1 = ((_amount_1).doubleValue() != 0);
            _or = (_notEquals || _notEquals_1);
          }
          _and = (_equals_1 && _or);
        }
        if (_and) {
          _xifexpression_1 = false;
        } else {
          final Function1<RCProductPurchaseItem,Boolean> _function = new Function1<RCProductPurchaseItem,Boolean>() {
              public Boolean apply(final RCProductPurchaseItem it) {
                Integer _orderQuantity = it.getOrderQuantity();
                Integer _quantity = it.getQuantity();
                boolean _greaterEqualsThan = (_orderQuantity.compareTo(_quantity) >= 0);
                return Boolean.valueOf(_greaterEqualsThan);
              }
            };
          boolean _forall = IterableExtensions.<RCProductPurchaseItem>forall(items, _function);
          _xifexpression_1 = _forall;
        }
        _xblockexpression = (_xifexpression_1);
      }
      _xifexpression = _xblockexpression;
    } else {
      RCBudgetStatus _budgetStatus_1 = this.getBudgetStatus();
      boolean _equals_1 = Objects.equal(_budgetStatus_1, RCBudgetStatus.finished);
      _xifexpression = _equals_1;
    }
    return Boolean.valueOf(_xifexpression);
  }
  
  public RCBudgetTaskStatus getBudgetTaskStatus() {
    RCBudgetTaskStatus _xifexpression = null;
    boolean _and = false;
    RCProjectMoney _contractPrice = this.getContractPrice();
    Double _amount = _contractPrice.getAmount();
    boolean _equals = ((_amount).doubleValue() == 0d);
    if (!_equals) {
      _and = false;
    } else {
      RCProjectMoney _netPrice = this.getNetPrice();
      Double _amount_1 = _netPrice.getAmount();
      boolean _equals_1 = ((_amount_1).doubleValue() == 0);
      _and = (_equals && _equals_1);
    }
    if (_and) {
      _xifexpression = RCBudgetTaskStatus.unknown;
    } else {
      RCBudgetTaskStatus _xifexpression_1 = null;
      Boolean _seemsFinished = this.seemsFinished();
      if ((_seemsFinished).booleanValue()) {
        _xifexpression_1 = RCBudgetTaskStatus.finished;
      } else {
        _xifexpression_1 = RCBudgetTaskStatus.unfinished;
      }
      _xifexpression = _xifexpression_1;
    }
    return _xifexpression;
  }
  
  /**
   * 根据发货记录计算指定产品的发货总数
   */
  public Integer getTotalDeliveredQuantity(final RCProductSpecification productSpec) {
    int _xblockexpression = (int) 0;
    {
      Collection<RCBudgetContract> _contracts = this.getContracts();
      final Function1<RCBudgetContract,Collection<RCProductDeliver>> _function = new Function1<RCBudgetContract,Collection<RCProductDeliver>>() {
          public Collection<RCProductDeliver> apply(final RCBudgetContract it) {
            Collection<RCProductDeliver> _delivers = it.getDelivers();
            return _delivers;
          }
        };
      Iterable<Collection<RCProductDeliver>> _map = IterableExtensions.<RCBudgetContract, Collection<RCProductDeliver>>map(_contracts, _function);
      Iterable<RCProductDeliver> _flatten = Iterables.<RCProductDeliver>concat(_map);
      final Function1<RCProductDeliver,Collection<RCProductDeliverItem>> _function_1 = new Function1<RCProductDeliver,Collection<RCProductDeliverItem>>() {
          public Collection<RCProductDeliverItem> apply(final RCProductDeliver it) {
            Collection<RCProductDeliverItem> _items = it.getItems();
            return _items;
          }
        };
      Iterable<Collection<RCProductDeliverItem>> _map_1 = IterableExtensions.<RCProductDeliver, Collection<RCProductDeliverItem>>map(_flatten, _function_1);
      Iterable<RCProductDeliverItem> _flatten_1 = Iterables.<RCProductDeliverItem>concat(_map_1);
      final Function1<RCProductDeliverItem,Boolean> _function_2 = new Function1<RCProductDeliverItem,Boolean>() {
          public Boolean apply(final RCProductDeliverItem it) {
            RCProductSpecification _productSpec = it.getProductSpec();
            boolean _equals = Objects.equal(_productSpec, productSpec);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCProductDeliverItem> _filter = IterableExtensions.<RCProductDeliverItem>filter(_flatten_1, _function_2);
      final Function1<RCProductDeliverItem,Integer> _function_3 = new Function1<RCProductDeliverItem,Integer>() {
          public Integer apply(final RCProductDeliverItem it) {
            Integer _deliveredQuantity = it.getDeliveredQuantity();
            return _deliveredQuantity;
          }
        };
      Iterable<Integer> _map_2 = IterableExtensions.<RCProductDeliverItem, Integer>map(_filter, _function_3);
      final Function2<Integer,Integer,Integer> _function_4 = new Function2<Integer,Integer,Integer>() {
          public Integer apply(final Integer i1, final Integer i2) {
            int _plus = ((i1).intValue() + (i2).intValue());
            return Integer.valueOf(_plus);
          }
        };
      Integer result = IterableExtensions.<Integer>reduce(_map_2, _function_4);
      int _xifexpression = (int) 0;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Integer.valueOf(_xblockexpression);
  }
  
  /**
   * 计算指定产品的请购数量
   */
  public Integer getTotalRequestedQuantity(final RCProductSpecification productSpec) {
    int _xblockexpression = (int) 0;
    {
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      Iterable<RCProductPurchaseRequest> _filter = Iterables.<RCProductPurchaseRequest>filter(_purchaseRequests, RCProductPurchaseRequest.class);
      final Function1<RCProductPurchaseRequest,Boolean> _function = new Function1<RCProductPurchaseRequest,Boolean>() {
          public Boolean apply(final RCProductPurchaseRequest it) {
            RCProductPurchaseRequestStatus _status = it.getStatus();
            boolean _equals = Objects.equal(_status, RCProductPurchaseRequestStatus.canceled);
            boolean _not = (!_equals);
            return Boolean.valueOf(_not);
          }
        };
      Iterable<RCProductPurchaseRequest> _filter_1 = IterableExtensions.<RCProductPurchaseRequest>filter(_filter, _function);
      final Function1<RCProductPurchaseRequest,Collection<RCProductPurchaseRequestItem>> _function_1 = new Function1<RCProductPurchaseRequest,Collection<RCProductPurchaseRequestItem>>() {
          public Collection<RCProductPurchaseRequestItem> apply(final RCProductPurchaseRequest it) {
            Collection<RCProductPurchaseRequestItem> _items = it.getItems();
            return _items;
          }
        };
      Iterable<Collection<RCProductPurchaseRequestItem>> _map = IterableExtensions.<RCProductPurchaseRequest, Collection<RCProductPurchaseRequestItem>>map(_filter_1, _function_1);
      Iterable<RCProductPurchaseRequestItem> _flatten = Iterables.<RCProductPurchaseRequestItem>concat(_map);
      final Function1<RCProductPurchaseRequestItem,Boolean> _function_2 = new Function1<RCProductPurchaseRequestItem,Boolean>() {
          public Boolean apply(final RCProductPurchaseRequestItem it) {
            RCProductSpecification _productSpec = it.getProductSpec();
            boolean _equals = Objects.equal(_productSpec, productSpec);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCProductPurchaseRequestItem> _filter_2 = IterableExtensions.<RCProductPurchaseRequestItem>filter(_flatten, _function_2);
      final Function1<RCProductPurchaseRequestItem,Integer> _function_3 = new Function1<RCProductPurchaseRequestItem,Integer>() {
          public Integer apply(final RCProductPurchaseRequestItem it) {
            Integer _quantity = it.getQuantity();
            return _quantity;
          }
        };
      Iterable<Integer> _map_1 = IterableExtensions.<RCProductPurchaseRequestItem, Integer>map(_filter_2, _function_3);
      final Function2<Integer,Integer,Integer> _function_4 = new Function2<Integer,Integer,Integer>() {
          public Integer apply(final Integer i1, final Integer i2) {
            int _plus = ((i1).intValue() + (i2).intValue());
            return Integer.valueOf(_plus);
          }
        };
      Integer result = IterableExtensions.<Integer>reduce(_map_1, _function_4);
      int _xifexpression = (int) 0;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Integer.valueOf(_xblockexpression);
  }
  
  /**
   * 已订购数量
   */
  public Integer getTotalPassedOrderedQuantity(final RCProductSpecification productSpec) {
    int _xblockexpression = (int) 0;
    {
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      Iterable<RCProductPurchaseRequest> _filter = Iterables.<RCProductPurchaseRequest>filter(_purchaseRequests, RCProductPurchaseRequest.class);
      final Function1<RCProductPurchaseRequest,Boolean> _function = new Function1<RCProductPurchaseRequest,Boolean>() {
          public Boolean apply(final RCProductPurchaseRequest it) {
            boolean _and = false;
            Boolean _isPassed = it.getIsPassed();
            boolean _notEquals = (!Objects.equal(_isPassed, null));
            if (!_notEquals) {
              _and = false;
            } else {
              Boolean _isPassed_1 = it.getIsPassed();
              _and = (_notEquals && (_isPassed_1).booleanValue());
            }
            return Boolean.valueOf(_and);
          }
        };
      Iterable<RCProductPurchaseRequest> _filter_1 = IterableExtensions.<RCProductPurchaseRequest>filter(_filter, _function);
      final Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>> _function_1 = new Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>>() {
          public Collection<RCPurchaseOrder> apply(final RCProductPurchaseRequest it) {
            Collection<RCPurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<RCPurchaseOrder>> _map = IterableExtensions.<RCProductPurchaseRequest, Collection<RCPurchaseOrder>>map(_filter_1, _function_1);
      Iterable<RCPurchaseOrder> _flatten = Iterables.<RCPurchaseOrder>concat(_map);
      final Function1<RCPurchaseOrder,Boolean> _function_2 = new Function1<RCPurchaseOrder,Boolean>() {
          public Boolean apply(final RCPurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<RCPurchaseOrder> _filter_2 = IterableExtensions.<RCPurchaseOrder>filter(_flatten, _function_2);
      final Function1<RCPurchaseOrder,Collection<RCPurchaseOrderItem>> _function_3 = new Function1<RCPurchaseOrder,Collection<RCPurchaseOrderItem>>() {
          public Collection<RCPurchaseOrderItem> apply(final RCPurchaseOrder it) {
            Collection<RCPurchaseOrderItem> _orderItems = it.getOrderItems();
            return _orderItems;
          }
        };
      Iterable<Collection<RCPurchaseOrderItem>> _map_1 = IterableExtensions.<RCPurchaseOrder, Collection<RCPurchaseOrderItem>>map(_filter_2, _function_3);
      Iterable<RCPurchaseOrderItem> orderItems = Iterables.<RCPurchaseOrderItem>concat(_map_1);
      final Function1<RCPurchaseOrderItem,Boolean> _function_4 = new Function1<RCPurchaseOrderItem,Boolean>() {
          public Boolean apply(final RCPurchaseOrderItem it) {
            RCProductSpecification _productSpec = it.getProductSpec();
            boolean _equals = Objects.equal(_productSpec, productSpec);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCPurchaseOrderItem> _filter_3 = IterableExtensions.<RCPurchaseOrderItem>filter(orderItems, _function_4);
      final Function1<RCPurchaseOrderItem,Integer> _function_5 = new Function1<RCPurchaseOrderItem,Integer>() {
          public Integer apply(final RCPurchaseOrderItem it) {
            Integer _quantity = it.getQuantity();
            return _quantity;
          }
        };
      Iterable<Integer> _map_2 = IterableExtensions.<RCPurchaseOrderItem, Integer>map(_filter_3, _function_5);
      final Function2<Integer,Integer,Integer> _function_6 = new Function2<Integer,Integer,Integer>() {
          public Integer apply(final Integer i1, final Integer i2) {
            int _plus = ((i1).intValue() + (i2).intValue());
            return Integer.valueOf(_plus);
          }
        };
      Integer result = IterableExtensions.<Integer>reduce(_map_2, _function_6);
      int _xifexpression = (int) 0;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Integer.valueOf(_xblockexpression);
  }
  
  /**
   * 已订购数量
   */
  public Integer getTotalOrderedQuantity(final RCProductSpecification productSpec) {
    int _xblockexpression = (int) 0;
    {
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      Iterable<RCProductPurchaseRequest> _filter = Iterables.<RCProductPurchaseRequest>filter(_purchaseRequests, RCProductPurchaseRequest.class);
      final Function1<RCProductPurchaseRequest,Boolean> _function = new Function1<RCProductPurchaseRequest,Boolean>() {
          public Boolean apply(final RCProductPurchaseRequest it) {
            RCProductPurchaseRequestStatus _status = it.getStatus();
            boolean _equals = Objects.equal(_status, RCProductPurchaseRequestStatus.canceled);
            boolean _not = (!_equals);
            return Boolean.valueOf(_not);
          }
        };
      Iterable<RCProductPurchaseRequest> _filter_1 = IterableExtensions.<RCProductPurchaseRequest>filter(_filter, _function);
      final Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>> _function_1 = new Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>>() {
          public Collection<RCPurchaseOrder> apply(final RCProductPurchaseRequest it) {
            Collection<RCPurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<RCPurchaseOrder>> _map = IterableExtensions.<RCProductPurchaseRequest, Collection<RCPurchaseOrder>>map(_filter_1, _function_1);
      Iterable<RCPurchaseOrder> _flatten = Iterables.<RCPurchaseOrder>concat(_map);
      final Function1<RCPurchaseOrder,Boolean> _function_2 = new Function1<RCPurchaseOrder,Boolean>() {
          public Boolean apply(final RCPurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<RCPurchaseOrder> _filter_2 = IterableExtensions.<RCPurchaseOrder>filter(_flatten, _function_2);
      final Function1<RCPurchaseOrder,Collection<RCPurchaseOrderItem>> _function_3 = new Function1<RCPurchaseOrder,Collection<RCPurchaseOrderItem>>() {
          public Collection<RCPurchaseOrderItem> apply(final RCPurchaseOrder it) {
            Collection<RCPurchaseOrderItem> _orderItems = it.getOrderItems();
            return _orderItems;
          }
        };
      Iterable<Collection<RCPurchaseOrderItem>> _map_1 = IterableExtensions.<RCPurchaseOrder, Collection<RCPurchaseOrderItem>>map(_filter_2, _function_3);
      Iterable<RCPurchaseOrderItem> orderItems = Iterables.<RCPurchaseOrderItem>concat(_map_1);
      final Function1<RCPurchaseOrderItem,Boolean> _function_4 = new Function1<RCPurchaseOrderItem,Boolean>() {
          public Boolean apply(final RCPurchaseOrderItem it) {
            RCProductSpecification _productSpec = it.getProductSpec();
            boolean _equals = Objects.equal(_productSpec, productSpec);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCPurchaseOrderItem> _filter_3 = IterableExtensions.<RCPurchaseOrderItem>filter(orderItems, _function_4);
      final Function1<RCPurchaseOrderItem,Integer> _function_5 = new Function1<RCPurchaseOrderItem,Integer>() {
          public Integer apply(final RCPurchaseOrderItem it) {
            Integer _quantity = it.getQuantity();
            return _quantity;
          }
        };
      Iterable<Integer> _map_2 = IterableExtensions.<RCPurchaseOrderItem, Integer>map(_filter_3, _function_5);
      final Function2<Integer,Integer,Integer> _function_6 = new Function2<Integer,Integer,Integer>() {
          public Integer apply(final Integer i1, final Integer i2) {
            int _plus = ((i1).intValue() + (i2).intValue());
            return Integer.valueOf(_plus);
          }
        };
      Integer result = IterableExtensions.<Integer>reduce(_map_2, _function_6);
      int _xifexpression = (int) 0;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Integer.valueOf(_xblockexpression);
  }
  
  /**
   * 根据收货记录计算指定产品的到货总数
   */
  public Integer getTotalAcceptedQuantity(final RCProductSpecification productSpec) {
    int _xblockexpression = (int) 0;
    {
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      Iterable<RCProductPurchaseRequest> _filter = Iterables.<RCProductPurchaseRequest>filter(_purchaseRequests, RCProductPurchaseRequest.class);
      final Function1<RCProductPurchaseRequest,Boolean> _function = new Function1<RCProductPurchaseRequest,Boolean>() {
          public Boolean apply(final RCProductPurchaseRequest it) {
            Boolean _isPassed = it.getIsPassed();
            return _isPassed;
          }
        };
      Iterable<RCProductPurchaseRequest> _filter_1 = IterableExtensions.<RCProductPurchaseRequest>filter(_filter, _function);
      final Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>> _function_1 = new Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>>() {
          public Collection<RCPurchaseOrder> apply(final RCProductPurchaseRequest it) {
            Collection<RCPurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<RCPurchaseOrder>> _map = IterableExtensions.<RCProductPurchaseRequest, Collection<RCPurchaseOrder>>map(_filter_1, _function_1);
      Iterable<RCPurchaseOrder> _flatten = Iterables.<RCPurchaseOrder>concat(_map);
      final Function1<RCPurchaseOrder,Boolean> _function_2 = new Function1<RCPurchaseOrder,Boolean>() {
          public Boolean apply(final RCPurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<RCPurchaseOrder> _filter_2 = IterableExtensions.<RCPurchaseOrder>filter(_flatten, _function_2);
      final Function1<RCPurchaseOrder,Integer> _function_3 = new Function1<RCPurchaseOrder,Integer>() {
          public Integer apply(final RCPurchaseOrder it) {
            Integer _totalAcceptItemsQuantity = it.getTotalAcceptItemsQuantity(productSpec);
            return _totalAcceptItemsQuantity;
          }
        };
      Iterable<Integer> _map_1 = IterableExtensions.<RCPurchaseOrder, Integer>map(_filter_2, _function_3);
      final Function2<Integer,Integer,Integer> _function_4 = new Function2<Integer,Integer,Integer>() {
          public Integer apply(final Integer i1, final Integer i2) {
            int _plus = ((i1).intValue() + (i2).intValue());
            return Integer.valueOf(_plus);
          }
        };
      Integer result = IterableExtensions.<Integer>reduce(_map_1, _function_4);
      int _xifexpression = (int) 0;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Integer.valueOf(_xblockexpression);
  }
  
  /**
   * 根据合同计算指定产品需求总数
   */
  public Integer getTotalRequiredQuantity(final RCProductSpecification productSpec) {
    int _xblockexpression = (int) 0;
    {
      Collection<RCBudgetContract> _contracts = this.getContracts();
      final Function1<RCBudgetContract,Collection<RCBudgetContractItem>> _function = new Function1<RCBudgetContract,Collection<RCBudgetContractItem>>() {
          public Collection<RCBudgetContractItem> apply(final RCBudgetContract it) {
            Collection<RCBudgetContractItem> _contratItems = it.getContratItems();
            return _contratItems;
          }
        };
      Iterable<Collection<RCBudgetContractItem>> _map = IterableExtensions.<RCBudgetContract, Collection<RCBudgetContractItem>>map(_contracts, _function);
      Iterable<RCBudgetContractItem> _flatten = Iterables.<RCBudgetContractItem>concat(_map);
      final Function1<RCBudgetContractItem,Boolean> _function_1 = new Function1<RCBudgetContractItem,Boolean>() {
          public Boolean apply(final RCBudgetContractItem it) {
            RCProductSpecification _productSpec = it.getProductSpec();
            boolean _equals = Objects.equal(_productSpec, productSpec);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCBudgetContractItem> _filter = IterableExtensions.<RCBudgetContractItem>filter(_flatten, _function_1);
      final Function1<RCBudgetContractItem,Integer> _function_2 = new Function1<RCBudgetContractItem,Integer>() {
          public Integer apply(final RCBudgetContractItem it) {
            Integer _quantity = it.getQuantity();
            return _quantity;
          }
        };
      Iterable<Integer> _map_1 = IterableExtensions.<RCBudgetContractItem, Integer>map(_filter, _function_2);
      final Function2<Integer,Integer,Integer> _function_3 = new Function2<Integer,Integer,Integer>() {
          public Integer apply(final Integer i1, final Integer i2) {
            int _plus = ((i1).intValue() + (i2).intValue());
            return Integer.valueOf(_plus);
          }
        };
      Integer result = IterableExtensions.<Integer>reduce(_map_1, _function_3);
      int _xifexpression = (int) 0;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Integer.valueOf(_xblockexpression);
  }
  
  /**
   * 根据合同计算指定产品需求总金额
   */
  public RCProjectMoney getTotalRequiredAmount(final RCProductSpecification productSpec) {
    Collection<RCBudgetContract> _contracts = this.getContracts();
    final Function1<RCBudgetContract,RCProjectMoney> _function = new Function1<RCBudgetContract,RCProjectMoney>() {
        public RCProjectMoney apply(final RCBudgetContract it) {
          RCProjectMoney _totalListAmount = it.getTotalListAmount(productSpec);
          return _totalListAmount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCBudgetContract, RCProjectMoney>map(_contracts, _function);
    RCExchangeRate _defaultContractExchangeRate = this.getDefaultContractExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultContractExchangeRate);
    return _sum;
  }
  
  /**
   * 计算手续费总金额
   */
  public RCProjectMoney getTotalOtherAmount() {
    RCProjectMoney _xblockexpression = null;
    {
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      final Function1<RCBasePurchaseRequest,Boolean> _function = new Function1<RCBasePurchaseRequest,Boolean>() {
          public Boolean apply(final RCBasePurchaseRequest it) {
            Boolean _isPassed = it.getIsPassed();
            return _isPassed;
          }
        };
      Iterable<RCBasePurchaseRequest> _filter = IterableExtensions.<RCBasePurchaseRequest>filter(_purchaseRequests, _function);
      final Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>> _function_1 = new Function1<RCBasePurchaseRequest,Collection<? extends RCBasePurchaseOrder>>() {
          public Collection<? extends RCBasePurchaseOrder> apply(final RCBasePurchaseRequest it) {
            Collection<? extends RCBasePurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<? extends RCBasePurchaseOrder>> _map = IterableExtensions.<RCBasePurchaseRequest, Collection<? extends RCBasePurchaseOrder>>map(_filter, _function_1);
      Iterable<? extends RCBasePurchaseOrder> _flatten = Iterables.concat(_map);
      final Function1<RCBasePurchaseOrder,Boolean> _function_2 = new Function1<RCBasePurchaseOrder,Boolean>() {
          public Boolean apply(final RCBasePurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<? extends RCBasePurchaseOrder> orders = IterableExtensions.filter(_flatten, _function_2);
      final Function1<RCBasePurchaseOrder,Collection<RCBasePurchasePayRequest>> _function_3 = new Function1<RCBasePurchaseOrder,Collection<RCBasePurchasePayRequest>>() {
          public Collection<RCBasePurchasePayRequest> apply(final RCBasePurchaseOrder it) {
            Collection<RCBasePurchasePayRequest> _payRequests = it.getPayRequests();
            return _payRequests;
          }
        };
      Iterable<Collection<RCBasePurchasePayRequest>> _map_1 = IterableExtensions.map(orders, _function_3);
      Iterable<RCBasePurchasePayRequest> _flatten_1 = Iterables.<RCBasePurchasePayRequest>concat(_map_1);
      Iterable<RCOrderCommissionPayRequest> _filter_1 = Iterables.<RCOrderCommissionPayRequest>filter(_flatten_1, RCOrderCommissionPayRequest.class);
      final Function1<RCOrderCommissionPayRequest,RCProjectMoney> _function_4 = new Function1<RCOrderCommissionPayRequest,RCProjectMoney>() {
          public RCProjectMoney apply(final RCOrderCommissionPayRequest it) {
            RCProjectMoney _totalPaidProjectAmount = it.getTotalPaidProjectAmount();
            return _totalPaidProjectAmount;
          }
        };
      Iterable<RCProjectMoney> _map_2 = IterableExtensions.<RCOrderCommissionPayRequest, RCProjectMoney>map(_filter_1, _function_4);
      RCExchangeRate _defaultPurchaseExchangeRate = this.getDefaultPurchaseExchangeRate();
      final RCProjectMoney r1 = Moneys.sum(_map_2, _defaultPurchaseExchangeRate);
      final Function1<RCTransforCostPayRequest,RCProjectMoney> _function_5 = new Function1<RCTransforCostPayRequest,RCProjectMoney>() {
          public RCProjectMoney apply(final RCTransforCostPayRequest it) {
            RCProjectMoney _totalPaidProjectAmount = it.getTotalPaidProjectAmount();
            return _totalPaidProjectAmount;
          }
        };
      Iterable<RCProjectMoney> _map_3 = IterableExtensions.<RCTransforCostPayRequest, RCProjectMoney>map(this.transforCostPayRequest, _function_5);
      RCExchangeRate _defaultPurchaseExchangeRate_1 = this.getDefaultPurchaseExchangeRate();
      final RCProjectMoney r2 = Moneys.sum(_map_3, _defaultPurchaseExchangeRate_1);
      RCProjectMoney _plus = r1.plus(r2);
      _xblockexpression = (_plus);
    }
    return _xblockexpression;
  }
  
  public RCProductDirectCostRequest createProductDirectCostRequest(final Procedure1<? super RCProductDirectCostRequest> init) {RCProductDirectCostRequest rCProductDirectCostRequest=RCProductDirectCostRequestImpl.create(this,getObjectFactory());
    final Procedure1<RCProductDirectCostRequest> _function = new Procedure1<RCProductDirectCostRequest>() {
        public void apply(final RCProductDirectCostRequest it) {
          BeanFactory _beanFactory = RCBaseProductPurchaseBudgetImpl.this.getBeanFactory();
          ISystemUserService _bean = _beanFactory.<ISystemUserService>getBean(ISystemUserService.class);
          Party _currentUserAsParty = _bean.getCurrentUserAsParty();
          it.setRequester(((RCEmployee) _currentUserAsParty));
          it.setIsFinished(Boolean.valueOf(true));
          Date _now = DateUtil.now();
          it.setFinishDate(_now);
          RCPay _createAndAddtoPurchasePays = it.createAndAddtoPurchasePays(RCBaseProductPurchaseBudgetImpl.this);
          final Procedure1<RCPay> _function = new Procedure1<RCPay>() {
              public void apply(final RCPay it) {
                Date _now = DateUtil.now();
                it.setPayDate(_now);
              }
            };
          ObjectExtensions.<RCPay>operator_doubleArrow(_createAndAddtoPurchasePays, _function);
          init.apply(it);
          RCCostBreakDown _cbd = RCBaseProductPurchaseBudgetImpl.this.getCbd();
          RCProject _project = _cbd.getProject();
          RCProjectStat _stat = _project.getStat();
          _stat.updateInfo();
        }
      };
    RCProductDirectCostRequest _doubleArrow = ObjectExtensions.<RCProductDirectCostRequest>operator_doubleArrow(rCProductDirectCostRequest, _function);
    return _doubleArrow;
  }
  
  public void updateProductDirectCostRequest(final RCProductDirectCostRequest value, final Procedure1<? super RCProductDirectCostRequest> updator) {
    updator.apply(value);
    RCCostBreakDown _cbd = this.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
  }
  
  public void deleteProductDirectCostRequest(final RCProductDirectCostRequest value) {getObjectFactory().delete(value);
    
    RCCostBreakDown _cbd = this.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectStat _stat = _project.getStat();
    _stat.updateInfo();
  }
  
  /**
   * 服务订单金额
   */
  public RCProjectMoney getTotalOrderAmount4Service() {
    RCCostBreakDown _cbd = this.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectMoney _zeroAmount = _project.getZeroAmount();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          Double _totalACTOrderAmount4Service = RCBaseProductPurchaseBudgetImpl.this.getTotalACTOrderAmount4Service();
          it.setAmount(_totalACTOrderAmount4Service);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount, _function);
    return _doubleArrow;
  }
  
  /**
   * 产品订单金额
   */
  public RCProjectMoney getTotalOrderAmount4Product() {
    RCCostBreakDown _cbd = this.getCbd();
    RCProject _project = _cbd.getProject();
    RCProjectMoney _zeroAmount = _project.getZeroAmount();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          Double _totalACTOrderAmount4Product = RCBaseProductPurchaseBudgetImpl.this.getTotalACTOrderAmount4Product();
          it.setAmount(_totalACTOrderAmount4Product);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_zeroAmount, _function);
    return _doubleArrow;
  }
  
  /**
   * 服务订单相关金额
   */
  public Double getTotalACTOrderAmount4Service() {
    Iterable<RCCost4ACTItem> _cost4ServiceACItems = this.getCost4ServiceACItems();
    final Function1<RCCost4ACTItem,Double> _function = new Function1<RCCost4ACTItem,Double>() {
        public Double apply(final RCCost4ACTItem it) {
          Double _amount = it.getAmount();
          return _amount;
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCCost4ACTItem, Double>map(_cost4ServiceACItems, _function);
    final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  /**
   * 产品订单相关金额
   */
  public Double getTotalACTOrderAmount4Product() {
    Iterable<RCCost4ACTItem> _cost4ProductACItems = this.getCost4ProductACItems();
    final Function1<RCCost4ACTItem,Double> _function = new Function1<RCCost4ACTItem,Double>() {
        public Double apply(final RCCost4ACTItem it) {
          Double _amount = it.getAmount();
          return _amount;
        }
      };
    Iterable<Double> _map = IterableExtensions.<RCCost4ACTItem, Double>map(_cost4ProductACItems, _function);
    final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
        public Double apply(final Double d1, final Double d2) {
          double _plus = DoubleExtensions.operator_plus(d1, d2);
          return Double.valueOf(_plus);
        }
      };
    Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
    Double _nullsafe = NumberExtensions.nullsafe(_reduce);
    return _nullsafe;
  }
  
  /**
   * 汇总合同中的产品型号
   */
  public Collection<RCProductSpecification> getAllProductSpecifications() {
    Collection<RCBudgetContract> _contracts = this.getContracts();
    final Function1<RCBudgetContract,Collection<RCBudgetContractItem>> _function = new Function1<RCBudgetContract,Collection<RCBudgetContractItem>>() {
        public Collection<RCBudgetContractItem> apply(final RCBudgetContract it) {
          Collection<RCBudgetContractItem> _contratItems = it.getContratItems();
          return _contratItems;
        }
      };
    Iterable<Collection<RCBudgetContractItem>> _map = IterableExtensions.<RCBudgetContract, Collection<RCBudgetContractItem>>map(_contracts, _function);
    Iterable<RCBudgetContractItem> _flatten = Iterables.<RCBudgetContractItem>concat(_map);
    final Function1<RCBudgetContractItem,RCProductSpecification> _function_1 = new Function1<RCBudgetContractItem,RCProductSpecification>() {
        public RCProductSpecification apply(final RCBudgetContractItem it) {
          RCProductSpecification _productSpec = it.getProductSpec();
          return _productSpec;
        }
      };
    Iterable<RCProductSpecification> _map_1 = IterableExtensions.<RCBudgetContractItem, RCProductSpecification>map(_flatten, _function_1);
    Set<RCProductSpecification> _set = IterableExtensions.<RCProductSpecification>toSet(_map_1);
    return _set;
  }
  
  public Iterable<RCCost4ACTItem> getCost4ServiceACItems() {
    Iterable<RCCost4ACTItem> _xblockexpression = null;
    {
      Collection<RCHasCostBudgetCostRequest> _costRequests = this.getCostRequests();
      Iterable<RCProductDirectCostRequest> _filter = Iterables.<RCProductDirectCostRequest>filter(_costRequests, RCProductDirectCostRequest.class);
      final Function1<RCProductDirectCostRequest,Boolean> _function = new Function1<RCProductDirectCostRequest,Boolean>() {
          public Boolean apply(final RCProductDirectCostRequest it) {
            RCProductDirectCostRequestType _requestType = it.getRequestType();
            boolean _equals = Objects.equal(_requestType, RCProductDirectCostRequestType.service);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCProductDirectCostRequest> _filter_1 = IterableExtensions.<RCProductDirectCostRequest>filter(_filter, _function);
      final Function1<RCProductDirectCostRequest,Iterable<RCCost4ACTItem>> _function_1 = new Function1<RCProductDirectCostRequest,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final RCProductDirectCostRequest it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map = IterableExtensions.<RCProductDirectCostRequest, Iterable<RCCost4ACTItem>>map(_filter_1, _function_1);
      final Iterable<RCCost4ACTItem> directCosts = Iterables.<RCCost4ACTItem>concat(_map);
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      Iterable<RCServiceProductPurchaseRequest> _filter_2 = Iterables.<RCServiceProductPurchaseRequest>filter(_purchaseRequests, RCServiceProductPurchaseRequest.class);
      final Function1<RCServiceProductPurchaseRequest,Boolean> _function_2 = new Function1<RCServiceProductPurchaseRequest,Boolean>() {
          public Boolean apply(final RCServiceProductPurchaseRequest it) {
            Boolean _isPassed = it.getIsPassed();
            return _isPassed;
          }
        };
      Iterable<RCServiceProductPurchaseRequest> _filter_3 = IterableExtensions.<RCServiceProductPurchaseRequest>filter(_filter_2, _function_2);
      final Function1<RCServiceProductPurchaseRequest,Collection<RCServicePurchaseOrder>> _function_3 = new Function1<RCServiceProductPurchaseRequest,Collection<RCServicePurchaseOrder>>() {
          public Collection<RCServicePurchaseOrder> apply(final RCServiceProductPurchaseRequest it) {
            Collection<RCServicePurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<RCServicePurchaseOrder>> _map_1 = IterableExtensions.<RCServiceProductPurchaseRequest, Collection<RCServicePurchaseOrder>>map(_filter_3, _function_3);
      Iterable<RCServicePurchaseOrder> _flatten = Iterables.<RCServicePurchaseOrder>concat(_map_1);
      final Function1<RCServicePurchaseOrder,Boolean> _function_4 = new Function1<RCServicePurchaseOrder,Boolean>() {
          public Boolean apply(final RCServicePurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<RCServicePurchaseOrder> orders = IterableExtensions.<RCServicePurchaseOrder>filter(_flatten, _function_4);
      final Function1<RCServicePurchaseOrder,Iterable<RCCost4ACTItem>> _function_5 = new Function1<RCServicePurchaseOrder,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final RCServicePurchaseOrder it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map_2 = IterableExtensions.<RCServicePurchaseOrder, Iterable<RCCost4ACTItem>>map(orders, _function_5);
      Iterable<RCCost4ACTItem> _flatten_1 = Iterables.<RCCost4ACTItem>concat(_map_2);
      ArrayList<Iterable<RCCost4ACTItem>> _newArrayList = CollectionLiterals.<Iterable<RCCost4ACTItem>>newArrayList(_flatten_1, directCosts);
      Iterable<RCCost4ACTItem> _flatten_2 = Iterables.<RCCost4ACTItem>concat(_newArrayList);
      _xblockexpression = (_flatten_2);
    }
    return _xblockexpression;
  }
  
  public Iterable<RCCost4ACTItem> getCost4ProductACItems() {
    Iterable<RCCost4ACTItem> _xblockexpression = null;
    {
      Collection<RCHasCostBudgetCostRequest> _costRequests = this.getCostRequests();
      Iterable<RCProductDirectCostRequest> _filter = Iterables.<RCProductDirectCostRequest>filter(_costRequests, RCProductDirectCostRequest.class);
      final Function1<RCProductDirectCostRequest,Boolean> _function = new Function1<RCProductDirectCostRequest,Boolean>() {
          public Boolean apply(final RCProductDirectCostRequest it) {
            RCProductDirectCostRequestType _requestType = it.getRequestType();
            boolean _equals = Objects.equal(_requestType, RCProductDirectCostRequestType.product);
            return Boolean.valueOf(_equals);
          }
        };
      Iterable<RCProductDirectCostRequest> _filter_1 = IterableExtensions.<RCProductDirectCostRequest>filter(_filter, _function);
      final Function1<RCProductDirectCostRequest,Iterable<RCCost4ACTItem>> _function_1 = new Function1<RCProductDirectCostRequest,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final RCProductDirectCostRequest it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map = IterableExtensions.<RCProductDirectCostRequest, Iterable<RCCost4ACTItem>>map(_filter_1, _function_1);
      final Iterable<RCCost4ACTItem> directCosts = Iterables.<RCCost4ACTItem>concat(_map);
      Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
      Iterable<RCProductPurchaseRequest> _filter_2 = Iterables.<RCProductPurchaseRequest>filter(_purchaseRequests, RCProductPurchaseRequest.class);
      final Function1<RCProductPurchaseRequest,Boolean> _function_2 = new Function1<RCProductPurchaseRequest,Boolean>() {
          public Boolean apply(final RCProductPurchaseRequest it) {
            Boolean _isPassed = it.getIsPassed();
            return _isPassed;
          }
        };
      Iterable<RCProductPurchaseRequest> _filter_3 = IterableExtensions.<RCProductPurchaseRequest>filter(_filter_2, _function_2);
      final Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>> _function_3 = new Function1<RCProductPurchaseRequest,Collection<RCPurchaseOrder>>() {
          public Collection<RCPurchaseOrder> apply(final RCProductPurchaseRequest it) {
            Collection<RCPurchaseOrder> _purchaseOrders = it.getPurchaseOrders();
            return _purchaseOrders;
          }
        };
      Iterable<Collection<RCPurchaseOrder>> _map_1 = IterableExtensions.<RCProductPurchaseRequest, Collection<RCPurchaseOrder>>map(_filter_3, _function_3);
      Iterable<RCPurchaseOrder> _flatten = Iterables.<RCPurchaseOrder>concat(_map_1);
      final Function1<RCPurchaseOrder,Boolean> _function_4 = new Function1<RCPurchaseOrder,Boolean>() {
          public Boolean apply(final RCPurchaseOrder it) {
            Boolean _isInitialized = it.getIsInitialized();
            return _isInitialized;
          }
        };
      Iterable<RCPurchaseOrder> orders = IterableExtensions.<RCPurchaseOrder>filter(_flatten, _function_4);
      final Function1<RCPurchaseOrder,Iterable<RCCost4ACTItem>> _function_5 = new Function1<RCPurchaseOrder,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final RCPurchaseOrder it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map_2 = IterableExtensions.<RCPurchaseOrder, Iterable<RCCost4ACTItem>>map(orders, _function_5);
      Iterable<RCCost4ACTItem> _flatten_1 = Iterables.<RCCost4ACTItem>concat(_map_2);
      ArrayList<Iterable<RCCost4ACTItem>> _newArrayList = CollectionLiterals.<Iterable<RCCost4ACTItem>>newArrayList(_flatten_1, directCosts);
      Iterable<RCCost4ACTItem> _flatten_2 = Iterables.<RCCost4ACTItem>concat(_newArrayList);
      _xblockexpression = (_flatten_2);
    }
    return _xblockexpression;
  }
  
  public Iterable<RCCost4ACTItem> getCost4ACItems() {
    Iterable<RCCost4ACTItem> _xblockexpression = null;
    {
      Iterable<RCCost4ACTItem> _cost4ServiceACItems = this.getCost4ServiceACItems();
      Iterable<RCCost4ACTItem> _cost4ProductACItems = this.getCost4ProductACItems();
      ArrayList<Iterable<RCCost4ACTItem>> _newArrayList = CollectionLiterals.<Iterable<RCCost4ACTItem>>newArrayList(_cost4ServiceACItems, _cost4ProductACItems);
      final Iterable<RCCost4ACTItem> c1 = Iterables.<RCCost4ACTItem>concat(_newArrayList);
      final Function1<RCTransforCostPayRequest,Iterable<RCCost4ACTItem>> _function = new Function1<RCTransforCostPayRequest,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final RCTransforCostPayRequest it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map = IterableExtensions.<RCTransforCostPayRequest, Iterable<RCCost4ACTItem>>map(this.transforCostPayRequest, _function);
      final Iterable<RCCost4ACTItem> c3 = Iterables.<RCCost4ACTItem>concat(_map);
      ArrayList<Iterable<RCCost4ACTItem>> _newArrayList_1 = CollectionLiterals.<Iterable<RCCost4ACTItem>>newArrayList(c1, c3);
      final Iterable<RCCost4ACTItem> result = Iterables.<RCCost4ACTItem>concat(_newArrayList_1);
      final Procedure1<RCCost4ACTItem> _function_1 = new Procedure1<RCCost4ACTItem>() {
          public void apply(final RCCost4ACTItem it) {
            it.setBudget(RCBaseProductPurchaseBudgetImpl.this);
          }
        };
      IterableExtensions.<RCCost4ACTItem>forEach(result, _function_1);
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public Collection<RCPay> getAllPurchasePay() {
    IGenericQuery<RCPay> query=getObjectFactory().createGenericQuery(RCPay.class,"select p from com.shrct.businessprocess.purchase.impl.RCPayImpl as p   where ((p.budget) = (:this)) and  p.active = 1      ").setParameter("this",this);
    Collection<RCPay> _list = query.list();
    return _list;
  }
  
  public Collection<RCProductDeliverItem> getDeliveredItems(final RCProductSpecification productSpec) {
    Collection<RCBudgetContract> _contracts = this.getContracts();
    final Function1<RCBudgetContract,Collection<RCProductDeliver>> _function = new Function1<RCBudgetContract,Collection<RCProductDeliver>>() {
        public Collection<RCProductDeliver> apply(final RCBudgetContract it) {
          Collection<RCProductDeliver> _delivers = it.getDelivers();
          return _delivers;
        }
      };
    Iterable<Collection<RCProductDeliver>> _map = IterableExtensions.<RCBudgetContract, Collection<RCProductDeliver>>map(_contracts, _function);
    Iterable<RCProductDeliver> _flatten = Iterables.<RCProductDeliver>concat(_map);
    final Function1<RCProductDeliver,Collection<RCProductDeliverItem>> _function_1 = new Function1<RCProductDeliver,Collection<RCProductDeliverItem>>() {
        public Collection<RCProductDeliverItem> apply(final RCProductDeliver it) {
          Collection<RCProductDeliverItem> _items = it.getItems();
          return _items;
        }
      };
    Iterable<Collection<RCProductDeliverItem>> _map_1 = IterableExtensions.<RCProductDeliver, Collection<RCProductDeliverItem>>map(_flatten, _function_1);
    Iterable<RCProductDeliverItem> _flatten_1 = Iterables.<RCProductDeliverItem>concat(_map_1);
    List<RCProductDeliverItem> _list = IterableExtensions.<RCProductDeliverItem>toList(_flatten_1);
    return _list;
  }
  
  public Collection<? extends RCProductInStockEvent> getInStockEvents(final RCProductSpecification productSpec) {
    Collection<RCProductInStockEvent> _xblockexpression = null;
    {
      final RCHasPurchaseBudget thisBudget = ((RCHasPurchaseBudget) this);
      IGenericQuery<RCProductInStockEvent> query=getObjectFactory().createGenericQuery(RCProductInStockEvent.class,"select e2 from com.shrct.businessprocess.purchase.impl.RCProductInStockEventImpl as e2   where (((e2.product.type) = (:productSpec)) and ((e2.orderItem.purchaseOrder.purchaseRequest.budget) = (:thisBudget))) and  e2.active = 1      ").setParameter("productSpec",productSpec).setParameter("thisBudget",thisBudget);
      Collection<RCProductInStockEvent> _list = query.list();
      _xblockexpression = (_list);
    }
    return _xblockexpression;
  }
  
  public Iterable<RCProductEntry> getAllAcceptedProducts() {
    Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
    Iterable<IHasAcceptedProducts> _filter = Iterables.<IHasAcceptedProducts>filter(_purchaseRequests, IHasAcceptedProducts.class);
    final Function1<IHasAcceptedProducts,Iterable<RCProductEntry>> _function = new Function1<IHasAcceptedProducts,Iterable<RCProductEntry>>() {
        public Iterable<RCProductEntry> apply(final IHasAcceptedProducts it) {
          Iterable<RCProductEntry> _allAcceptedProducts = it.getAllAcceptedProducts();
          return _allAcceptedProducts;
        }
      };
    Iterable<Iterable<RCProductEntry>> _map = IterableExtensions.<IHasAcceptedProducts, Iterable<RCProductEntry>>map(_filter, _function);
    Iterable<RCProductEntry> _flatten = Iterables.<RCProductEntry>concat(_map);
    return _flatten;
  }
  
  public String getEntityName() {
    String _name = this.getName();
    return _name;
  }
  
  public RCExchangeRate getDefaultPurchaseExchangeRate() {
    RCProjectMoney _netPrice = this.getNetPrice();
    RCExchangeRate _exchange = _netPrice.getExchange();
    return _exchange;
  }
  
  /**
   * 采购申请通过后调用
   */
  public void purchaseRequestAuditPassed(final RCProductPurchaseRequest purchaseRequest) {
  }
  
  public void diagnose(final IDiagnoseAcceptor acceptor) {
    super.diagnose(acceptor);
    Collection<RCProductSpecification> _allProductSpecifications = this.getAllProductSpecifications();
    final Procedure1<RCProductSpecification> _function = new Procedure1<RCProductSpecification>() {
        public void apply(final RCProductSpecification productSpec) {
          Integer _totalOrderedQuantity = RCBaseProductPurchaseBudgetImpl.this.getTotalOrderedQuantity(productSpec);
          Integer _totalRequestedQuantity = RCBaseProductPurchaseBudgetImpl.this.getTotalRequestedQuantity(productSpec);
          boolean _greaterThan = (_totalOrderedQuantity.compareTo(_totalRequestedQuantity) > 0);
          if (_greaterThan) {
            DiagnoseItem _diagnoseItem = new DiagnoseItem();
            final Procedure1<DiagnoseItem> _function = new Procedure1<DiagnoseItem>() {
                public void apply(final DiagnoseItem it) {
                  String _code = productSpec.getCode();
                  String _plus = ("\u4EA7\u54C1:[" + _code);
                  String _plus_1 = (_plus + "]\u8BA2\u8D2D\u6570\u91CF\u5927\u4E8E\u8BF7\u8D2D\u6570\u91CF");
                  it.setMessage(_plus_1);
                }
              };
            DiagnoseItem _doubleArrow = ObjectExtensions.<DiagnoseItem>operator_doubleArrow(_diagnoseItem, _function);
            acceptor.accept(_doubleArrow);
          }
        }
      };
    IterableExtensions.<RCProductSpecification>forEach(_allProductSpecifications, _function);
    Collection<RCBasePurchaseRequest> _purchaseRequests = this.getPurchaseRequests();
    final Procedure1<RCBasePurchaseRequest> _function_1 = new Procedure1<RCBasePurchaseRequest>() {
        public void apply(final RCBasePurchaseRequest it) {
          it.diagnose(acceptor);
        }
      };
    IterableExtensions.<RCBasePurchaseRequest>forEach(_purchaseRequests, _function_1);
  }
  
  public RCBaseProductPurchaseBudgetProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCBaseProductPurchaseBudgetProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCBaseProductPurchaseBudgetProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
